//
//  XMUserDetailImgsTableViewCell.m
//  XunMi
//
//  Created by MacBook on 2024/2/20.
//  Copyright © 2024 TieZhu. All rights reserved.
//

#import "XMUserDetailImgsTableViewCell.h"
#import <QuartzCore/QuartzCore.h>
#import <YBImageBrowser/YBImageBrowser.h>
#import "XMRecentPhotosModel.h"

@interface XMUserDetailImgsTableViewCell () <iCarouselDataSource, iCarouselDelegate>

@property (strong, nonatomic) iCarousel *carousel;

@property (nonatomic, strong) NSTimer *timer;

@end

@implementation XMUserDetailImgsTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.carousel = [[iCarousel alloc] initWithFrame:self.contentView.bounds];
    
    self.carousel.delegate = self;
    self.carousel.dataSource = self;
    self.type = iCarouselTypeRotary;
    self.carousel.scrollSpeed = 6;
    self.carousel.pagingEnabled = YES;
    [self.contentView addSubview:self.carousel];
    [self.carousel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView);
    }];
//    [self addTimer];
}

- (NSMutableArray *)dataArray {
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (void)setModel:(XMUserModel *)model {
    _model = model;
    [model.album_list enumerateObjectsUsingBlock:^(XMRecentPhotosModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (![XMUtil isBlankString:obj.image.file_url]) {
            [self.dataArray addObject:obj.image.file_url];
        }
    }];
    
    [self.carousel reloadData];
}

//- (void)setDataArray:(NSArray *)dataArray {
//    _dataArray = dataArray;
//    self.pageControl.numberOfPages = dataArray.count;
    //如果是网络的地址 就缓存
//    if([dataArray.firstObject hasPrefix:@"htttp"]){
//        [dataArray writeToFile:nil atomically:YES];
//    }
    
//    [self.carousel reloadData];
//}

- (void)setType:(iCarouselType)type {
    _type = type;
    self.carousel.type = type;
}

#pragma mark -- 私有
- (NSInteger)numberOfItemsInCarousel:(iCarousel *)carousel {
    return _dataArray.count;
}

//- (CGFloat)carouselItemWidth:(iCarousel *)carousel {
//
//    return 120;
//}

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view {
    if (view == nil) {
        UIView *myView = [[UIView alloc] initWithFrame:kFRAME(0, 0, 210, 280)];
        myView.backgroundColor = UIColor.whiteColor;
        // 添加阴影
        myView.layer.shadowColor = [UIColor blackColor].CGColor;
        myView.layer.shadowOffset = CGSizeMake(0, 2);  // 阴影偏移量
        myView.layer.shadowOpacity = 0.5;  // 阴影透明度
        myView.layer.shadowRadius = 5.0;  // 阴影半径
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
        [myView addSubview:imageView];
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(myView).insets(UIEdgeInsetsMake(5, 5, 5, 5));
        }];
        
        kCornerRadius(imageView, 0, 0, UIColor.clearColor);
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        NSString *str = _dataArray[index];
        if ([str hasPrefix:@"http"]) {
            [imageView sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:kDefaultCoverImg];
        } else {
            imageView.image = [UIImage imageNamed:str];
        }
        view = myView;
    }
    return view;
}

- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index {
    if (self.clickAction) {
        self.clickAction(index,self.dataArray);
    }
    NSMutableArray *datas = [NSMutableArray array];
    [self.dataArray enumerateObjectsUsingBlock:^(NSString *_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        // 网络图片
        YBIBImageData *data = [YBIBImageData new];
        data.imageURL = [NSURL URLWithString:obj];
        data.projectiveView = carousel.currentItemView;
        [datas addObject:data];
        
    }];
    YBImageBrowser *browser = [YBImageBrowser new];
    browser.dataSourceArray = datas;
    browser.currentPage = index;
    [browser show];
}

#pragma mark - iCarouselDelegate
//- (CATransform3D)carousel:(iCarousel *)carousel itemTransformForOffset:(CGFloat)offset baseTransform:(CATransform3D)transform {
//    static CGFloat max_sacle = 1.0f;
//    static CGFloat min_scale = 0.6f;
//    if (offset <= 1 && offset >= -1) {
//        float tempScale = offset < 0 ? 1+offset : 1-offset;
//        float slope = (max_sacle - min_scale) / 1;
//
//        CGFloat scale = min_scale + slope*tempScale;
//        transform = CATransform3DScale(transform, scale, scale, 1);
//    }else{
//        transform = CATransform3DScale(transform, min_scale, min_scale, 1);
//    }
//    return CATransform3DTranslate(transform, offset * self.carousel.itemWidth * 1.4, 0.0, 0.0);
//}

- (void)addTimer {
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0
                                                  target:self
                                                selector:@selector(nextImage)
                                                userInfo:nil
                                                 repeats:YES];
    //添加到runloop中
    [[NSRunLoop mainRunLoop]addTimer:self.timer
                             forMode:NSRunLoopCommonModes];
}

- (void)removeTimer {
    [self.timer invalidate];
    self.timer = nil;
}


- (void)nextImage {
    NSInteger index = self.carousel.currentItemIndex + 1;
    if (index == _dataArray.count ) {
        index = 0;
    }
    [self.carousel scrollToItemAtIndex:index
                              animated:YES];
}

- (void)carouselDidScroll:(iCarousel *)carousel {
//    self.pageControl.currentPage = carousel.currentItemIndex;
}

- (void)carouselWillBeginDragging:(iCarousel *)carousel{
//    [self removeTimer];
}

- (void)carouselDidEndDragging:(iCarousel *)carousel
                willDecelerate:(BOOL)decelerate{
    //    开启定时器
//    [self addTimer];
}

@end
